package com.example.demo.leetcode.level2;

import java.util.ArrayList;
import java.util.List;

public class 大数相乘_链表 {

    static class Node{
        int val;
        int digit;
        Node next;
        public Node(int val, int digit){
            this.val = val;
            this.digit = digit;
        }
    }

    public static String multiply(String num1, String num2) {

        if(num1.equals("0") || num2.equals("0")){
            return "0";
        }
        Node startA = new Node(-1,-1);
        Node startB = new Node(-1,-1);
        Node tempA = startA;
        Node tempB = startB;

        char[] chars = new StringBuffer(num1).reverse().toString().toCharArray();
        for(int i = 0; i < chars.length;i++){
            Node temp = new Node(chars[i]-'0',i);
            tempA.next = temp;
            tempA = tempA.next;
        }
        char[] charsB = new StringBuffer(num2).reverse().toString().toCharArray();
        for(int i = 0; i < charsB.length;i++){
            Node temp = new Node(charsB[i]-'0',i);
            tempB.next = temp;
            tempB = tempB.next;
        }

        tempA = startA.next;

        List<String>  resList = new ArrayList<>();
        String res = "0";
        while(tempA != null){
            tempB = startB.next;
            while(tempB != null){
                String val = tempA.val*tempB.val + "";
                for(int i=0;i<tempA.digit+tempB.digit;i++){
                    val = val + "0";
                }
                res = bigAdd(res, val);

                tempB = tempB.next;
            }
            tempA = tempA.next;
        }
        return res;
    }


    public static String bigAdd(String num1, String num2){

        Node startA = new Node(-1,-1);
        Node startB = new Node(-1,-1);
        Node tempA = startA;
        Node tempB = startB;

        char[] chars = new StringBuffer(num1).reverse().toString().toCharArray();
        for(int i = 0; i < chars.length;i++){
            Node temp = new Node(chars[i]-'0',0);
            tempA.next = temp;
            tempA = tempA.next;
        }
        char[] charsB = new StringBuffer(num2).reverse().toString().toCharArray();
        for(int i = 0; i < charsB.length;i++){
            Node temp = new Node(charsB[i]-'0',0);
            tempB.next = temp;
            tempB = tempB.next;
        }
        return new StringBuffer(add(startA.next, startB.next,"",0)).reverse().toString();
    }

    public static String add(Node nodeA, Node nodeB,String result, int other){

        while(nodeA != null && nodeB != null){
            int res = nodeA.val + nodeB.val + other;
            if(res >= 10){
               int jinwei = res / 10;
               res = res % 10 ;
                return add(nodeA.next, nodeB.next, result+res, jinwei);
            }else{
                return add(nodeA.next, nodeB.next, result+res, 0);
            }
        }

        while (nodeA != null) {
            int res = nodeA.val + other;
            if(res >= 10){
                int jinwei = res / 10;
                res = res % 10;
                return add(nodeA.next, null, result+res, jinwei);
            }else{
                return add(nodeA.next, null, result+res, 0);
            }
        }

        while (nodeB != null) {
            int res = nodeB.val + other;
            if(res >= 10){
                int jinwei = res / 10;
                res = res % 10;
                return add(null, nodeB.next, result+res, jinwei);
            }else{
                return add(null, nodeB.next, result+res, 0);
            }
        }
        while(other != 0){
            return result+other;
        }
        return result;
    }

    public static void main(String[] args) {
        String multiply = multiply("123", "456");
        System.out.println(multiply);
        // 123 456
//        System.out.println(bigAdd("0","18"));
//        System.out.println(bigAdd("18","120"));
//        System.out.println(bigAdd("138","600"));
//        System.out.println(bigAdd("738","150"));
//        System.out.println(bigAdd("888","1000"));
//        System.out.println(bigAdd("1888","5000"));
        System.out.println(bigAdd("6888","1200"));
        System.out.println(bigAdd("8088","8000"));
//        System.out.println(bigAdd("16088","40000"));
    }

}
